<script setup lang="ts">
import OrderCard from './OrderCard/index.vue'
import { ref, reactive, PropType } from 'vue'
import { doGetIcOrderList, doCancelTakeIcOrder, doUpdateOrderNextStatus } from '@/apis/order/sameCityOrder'
import { Address, IcOrderPageParams, PickPageParams, type IcOrder } from '@/apis/order/sameCityOrder/types'

const props = defineProps({
    status: {
        type: String as PropType<IcOrderPageParams['status']>,
        default: 'PENDING',
    },
    myLocation: {
        type: Array as unknown as PropType<Address['location']['coordinates']>,
        default: () => [],
    },
})
const emit = defineEmits(['navigation'])
const navigation = (item: IcOrder) => {
    emit('navigation', item)
}
const confirm = async (item: IcOrder) => {
    const { code } = await doUpdateOrderNextStatus(item.orderNo)
    if (code === 200) {
        uni.showToast({
            title: `已${item.status === 'TAKEN' ? '到店' : '取货'}`,
            icon: 'success',
            duration: 800,
        })
        // 更新列表
        if (item.status === 'TAKEN') {
            item.status = 'ARRIVED_SHOP'
        } else {
            IcOrderList.value = IcOrderList.value.filter((it) => it.orderNo !== item.orderNo)
            pageConfig.total--
        }
    }
}
const cancel = async (item: IcOrder) => {
    const { code } = await doCancelTakeIcOrder(item.orderNo)
    if (code === 200) {
        uni.showToast({
            title: '取消成功',
            icon: 'success',
            duration: 800,
        })
        // 更新列表
        IcOrderList.value = IcOrderList.value.filter((it) => it.orderNo !== item.orderNo)
        pageConfig.total--
    }
}

const IcOrderList = ref<IcOrder[]>([])
const pageConfig = reactive<PickPageParams<'current' | 'size' | 'pages' | 'total'>>({
    current: 1, // 当前页码
    size: 10, // 每页条数
    pages: 0, // 总页数
    total: 0, // 总条数
})
const handleGetIcOrderList = async () => {
    const { data } = await doGetIcOrderList({
        ...pageConfig,
        status: props.status,
    })
    if (data) {
        IcOrderList.value = [...IcOrderList.value, ...data!.records]
        pageConfig.current = data.current
        pageConfig.pages = data.pages
        pageConfig.total = data.total
    }
}
handleGetIcOrderList()

const loadMore = () => {
    if (IcOrderList.value.length >= pageConfig.total) return
    pageConfig.current += 1
    handleGetIcOrderList()
}
defineExpose({
    loadMore,
})
</script>

<template>
    <view v-if="IcOrderList.length">
        <OrderCard
            v-for="item in IcOrderList"
            :key="item.id"
            :orderItem="item"
            :text="`${item.status === 'TAKEN' ? '确认到店' : '确认取货'}`"
            :myLocation="myLocation"
            isShowTime
            @confirm="confirm(item)"
            @cancel="cancel(item)"
            @navigation="navigation(item)"
        >
        </OrderCard>
    </view>
    <block v-else>
        <u-empty
            text="暂无订单"
            mode="list"
            src="https://medusa-small-file-1253272780.cos.ap-shanghai.myqcloud.com/s2b2c_2.0.8/2024/11/146735d4e9e4b0dd23f7f6244b.png"
            iconSize="375rpx"
        ></u-empty>
    </block>
</template>

<style lang="scss" scoped></style>
